package net.xiduth.game.player.controlers;

import net.xiduth.game.Animation;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.npc.NPC;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;

/**
 * By Xiduth Development Team.
 */

public final  class LummyTutorialControler extends Controler {
   
   
      public LummyTutorialControler(){
      
      
      }
      @Override
  	public void start() {
    		NPC guide = findNPC(278);
			if (guide != null)
				player.getHintIconsManager().addHintIcon(guide, 0, -1, false);
			player.getDialogueManager().startDialogue("Tutorial");
  		moved();
  	}
      @Override
  	public boolean sendDeath() {
  		WorldTasksManager.schedule(new WorldTask() {
  			int loop;

  			@Override
  			public void run() {
  				player.stopAll();
  				if (loop == 0) {
  					player.setNextAnimation(new Animation(836));
  				} else if (loop == 1) {
  					player.getPackets().sendGameMessage(
  							"Oh dear, you have died.");
  				} else if (loop == 3) {
  					player.setNextAnimation(new Animation(-1));
  					player.reset();
  					player.setCanPvp(false);
  					player.setNextWorldTile(new WorldTile(3219, 3219, 0));
  					player.unlock();
  				}
  				loop++;
  			}
  		}, 0, 1);
  		return false;
  	}
      public int getStage() {
  		if (getArguments() == null)
  			setArguments(new Object[] { 0 }); 
  		return (Integer) getArguments()[0];
  	}

      public NPC findNPC(int id) {
  		for (NPC npc : World.getNPCs()) {
  			if (npc == null || npc.getId() != id)
  				continue;
  			return npc;
  		}
  		return null;
  	}
      
  	public void setStage(int stage) {
  		getArguments()[0] = stage;
  	}
	public void updateTutorial() {
		
	}
	public void refreshTutorial() {

	}
	@Override
	public void sendInterfaces() {
		
	}

	@Override
	public void magicTeleported(int tele) {
		exitTutorial();
	}

	public void exitTutorial() {
		player.getInterfaceManager().closeOverlay(false);
		removeControler();
		//Magic.sendNormalTeleportSpell(player, 0, 0,new WorldTile(x, y, h));
		//player.getPackets().sendGameMessage("You have been sent home.");
	}
	@Override
	public void moved() {
		boolean isInLummyTutorial = isInLummyTutorial(player);
		if(!isInLummyTutorial){
			player.setNextWorldTile(new WorldTile(3219, 3219, 0));
			player.getPackets().sendGameMessage("Sorry, you need to complete the tutorial before going anywhere.");
		}
	}

	public static final boolean isInLummyTutorial(WorldTile tile) {
		return (tile.getX() >= 3178 && tile.getX() <= 3268
				&& tile.getY() >= 3194 && tile.getY() <= 3300);
		
	}
      @Override
      public boolean processMagicTeleport(WorldTile toTile) {
         
		player.getPackets().sendGameMessage("Complete the tutorial first then use ::crystal.");
         return false;
      }
      @Override
      public boolean processItemTeleport(WorldTile toTile) {
         player.getPackets().sendGameMessage("Complete the tutorial first then use ::crystal.");
         return false;
      }
      @Override
      public boolean processObjectTeleport(WorldTile toTile) {
         player.getPackets().sendGameMessage("Complete the tutorial first then use ::crystal.");
         return false;
      }
	@Override
	public boolean login() {
		start();
		return false;
	}
	@Override
	public boolean logout() {
		return false;
	}
   
   }